require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIPriorityChange = {}
HelperFunc.SetUIEnv(UIPriorityChange)
local this = UIPriorityChange

function UIPriorityChange.Init(data, closeFunc)
    this.cur_data = data
    local _name, _package = getUIName(PanelNames.UIPriorityChange)
    panelMgr:ShowUI(_name, _package, this.onCreate, Config.useLocalUI)
end

function UIPriorityChange.onCreate(obj)
    this.root = obj
    if not this.hasInit then
        this.hasInit = true
        this.title = obj:GetChild("title")
        this.title.text = getLanguage("EquipAllocation5")
        this.btnUp2 = obj:GetChild("btnUp2")
        this.btnUp = obj:GetChild("btnUp")
        this.btnDown = obj:GetChild("btnDown")
        this.btnDown2 = obj:GetChild("btnDown2")
        this.typeController = obj:GetController("type")
        this.typeController:SetSelectedIndex(0)

        this.addEvent()
    end 
    this.initData()
end

function UIPriorityChange.refreshData( data )
    this.cur_data = data
    this.initData() 
end
function UIPriorityChange.initData()  
    if this.cur_data then
        if this.cur_data.index == 0 then
            this.typeController:SetSelectedIndex(0)
        elseif this.cur_data.index == 1 then
            this.typeController:SetSelectedIndex(1)
        else
            this.typeController:SetSelectedIndex(2)
        end
        this.root.x = this.cur_data.x + Config.uiXOffset
        this.root.y = this.cur_data.y
    end
end
function UIPriorityChange.OnEnable()
    ListenEvent(Config.EventType.Close_UIEquipmentAllocation, this.CloseSelf);
end

function UIPriorityChange.OnDisable()
    CloseEvent(Config.EventType.Close_UIEquipmentAllocation, this.CloseSelf);
end

function UIPriorityChange.addEvent() 
    this.root.onClick:Add(this.CloseSelf)
    this.btnUp2.onClick:Add(this.OnClickUp2)
    this.btnUp.onClick:Add(this.OnClickUp)
    this.btnDown.onClick:Add(this.OnClickDown)
    this.btnDown2.onClick:Add(this.OnClickDown2)

end

function UIPriorityChange.removeEvent() 
    this.root.onClick:Clear()
    this.btnUp2.onClick:Clear()
    this.btnUp.onClick:Clear()
    this.btnDown.onClick:Clear()
    this.btnDown2.onClick:Clear()

end
 
function UIPriorityChange.OnClickUp2() 
    this.OperateFunc(this.cur_data.item_info, this.cur_data.index, 0)
end
function UIPriorityChange.OnClickUp() 
    this.OperateFunc(this.cur_data.item_info, this.cur_data.index, this.cur_data.index-1)
end
function UIPriorityChange.OnClickDown() 
    this.OperateFunc(this.cur_data.item_info, this.cur_data.index, this.cur_data.index+1)
end
function UIPriorityChange.OnClickDown2() 
    this.OperateFunc(this.cur_data.item_info, this.cur_data.index, 2)
end

function UIPriorityChange.OperateFunc(item_info, index, next_index)
    if isOpenUI(PanelNames.UIEquipmentAllocation) then
        local ui_priority_change = CtrlManager.GetCtrl(PanelNames.UIEquipmentAllocation)
        ui_priority_change.ChangeItemInfo(item_info, index, next_index) 
    end
    this.CloseSelf()
end

function UIPriorityChange.CloseSelf()
    if isOpenUI(PanelNames.UIEquipmentAllocation) then
        local ui_priority_change = CtrlManager.GetCtrl(PanelNames.UIEquipmentAllocation) 
        ui_priority_change.clearData()
    end
    DestoryUI(PanelNames.UIPriorityChange)  
end

function UIPriorityChange.OnDestroy() 
    this.removeEvent()
    this.hasInit = nil
    this.root = nil
end

return UIPriorityChange
